import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    searchValue:"",
    imgSrcs: [],
    tabList: [{
      text: '精品',
      key: 0
    }, {
      text: '书签',
      key: 66
    }, {
      text: '笔',
      key: 1
    }, {
      text: '明信片',
      key: 3
    }, {
      text: '书',
      key: 4
    }, {
      text: '桌面摆件',
      key: 5
    }, {
      text: '笔记本',
      key: 6
    }, {
      text: '草稿本',
      key: 7
    }],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    cntList: [],
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: {
      value:0,
      label:'精品'
    },
  },
  search(e){
    let value = e.detail.value
    console.log('获取当前列表')
    let list = this.data.list
    console.log(this.privateData)
    const cntList = list.filter(item => item.title.includes(value));
    this.setData({
      cntList
    })
  },
  onShow() {
    this.init();
  },

  onLoad() {},

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },
  getGoodsList() {
    let _this = this
    wx.cloud.callFunction({
      name: 'getGoods'
    }).then(res => {
      console.log('data==>', res)
      let list = res.result.data.data
      _this.setData({
        list
      })
      _this.loadGoodsList()
    })
  },
  init() {
    this.loadHomePage();
    this.getGoodsList()
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize);
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
    console.log('获取当前列表')
    let list = this.data.list
    console.log(this.privateData)
    let i = 0,_this = this
    let cntList = list.filter(item => {
      if (_this.privateData.tabIndex.value == 0 && i++ < 10) { //前十个是精品
        return true;
      } else {
        return item.tag == _this.privateData.tabIndex.label;
      }
    });
    this.setData({
      cntList
    })
  },

  goodListClickHandle(e) {
    const {
      index
    } = e.detail;
    const {
      spuId
    } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`,
    });
  },
  goDetails(e) {
    let index = e.currentTarget.dataset.index
    console.log('当前点击的商品数据：', e.currentTarget.dataset.index)
    wx.setStorageSync("CntGood", this.data.cntList[index])

    wx.navigateTo({
      url: `/pages/goods/details/index`,
    });
  },
  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

  navToActivityDetail({
    detail
  }) {
    const {
      index: promotionID = 0
    } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
});